#include "GERM2Index.h"
#include "GINFOScreen.h"
#include "../std.h"
#include "../Widgets/Colours.h"
#include "../Widgets/UIBuilder.h"
#include "../Utilities/trim.h"
#include "../Utilities/Convert.h"

GERM2Index::GERM2Index(){

		mProcessor = new GLISTProcessor();

		isGetData = false;

		mLayout = createMainLayout(getStringNotIterative(MAIN_TEXT_START + 3), getStringNotIterative(L_SELECT), " ", getStringNotIterative(L_BACK));

		mTitle = (Label*)mLayout->getChildren()[0];

		mItems = (ListBox*)mLayout->getChildren()[1];

		mSoftkey = (Widget*)mLayout->getChildren()[2];

		this->setMain(mLayout);
}

GERM2Index::~GERM2Index(){
	delete mLayout;
}

void GERM2Index::keyPressEvent(int keyCode, int nativeCode)
{
	switch(keyCode)
	{
		case MAK_UP:
		case MAK_2:
			mItems->selectPreviousItem();
			break;
		case MAK_DOWN:
		case MAK_8:
			mItems->selectNextItem();
			break;
		case MAK_SOFTRIGHT:
		case MAK_6:
			APPCONTROLLER->back();
			break;
		case MAK_FIRE:
		case MAK_SOFTLEFT:{
			GINFOScreen* screen= (GINFOScreen*)APPCONTROLLER->getScreen("GINFOScreen");
			LRESItemWidget* widget = (LRESItemWidget*)mItems->getChildren()[mItems->getSelectedIndex()];
			String t = mTitle->getCaption() + "-" + widget->getCompanyName();
			if (mItems->getSelectedIndex() == 0){
				screen->setIsGetData(true);
				screen->setInput(widget->getId(), widget->getResult(), t);
			}else{
				screen->setIsGetData(false);
				screen->setInput(widget->getId(), widget->getResult(), t);
			}
			APPCONTROLLER->show("GINFOScreen");
		}
			break;
	}
}

void GERM2Index::itemReceived(GLISTItem& item){

	//lprintfln(item.digit.c_str());

	LRESItemWidget* newItem = new LRESItemWidget(0, 0, APPCONTROLLER->getScreenWidth(), HEIGHTITEM, mItems);

	newItem->setCompanyName(item.name);
	newItem->setCompanyFont(APPCONTROLLER->getFont("default"));
	//newItem->setDrawDate(item.digit);
	//newItem->setDrawDateFont(APPCONTROLLER->getFont("default"));

	newItem->setId(item.id);
	newItem->setResult(item.detail);

	//newItem->setSmsCnt(item.smscnt);
	//newItem->setShortCode(item.shortcode);
	//newItem->setIsOod(item.outofdate);

	newItem->setSkin(APPCONTROLLER->getSkin("default"));
	newItem->addWidgetListener(this);

	mItems->setSelectedIndex(0);
	mItems->getChildren()[0]->setSelected(true);

}

void GERM2Index::triggered(Widget* sender)
{
	//Act as if fire has been pressed
	//keyPressEvent(MAK_FIRE, 0);
}

void GERM2Index::show(){
	if (!isGetData){
		if (mItems->getChildren()[0] != NULL){
			mItems->setSelectedIndex(0);
		}
		mItems->clear();
		Label* l = (Label*)mSoftkey->getChildren()[1];
		l->setCaption("Loading");
		l->update();
		String url = "http://203.162.70.245:8314/WebService.asmx?WSDL";
		mProcessor->addRequest(url, this);
		isGetData = true;
	}else if (mItems->getChildren()[0] != NULL){
		mItems->setSelectedIndex(0);
		mItems->getChildren()[0]->setSelected(true);
	}
	Screen::show();
}

void GERM2Index::processingComplete(){
	Label* l = (Label*)mSoftkey->getChildren()[1];
	l->setCaption(" ");
	l->update();
	lprintfln("process GLIST complete");
}

void GERM2Index::pointerReleaseEvent(MAPoint2d point){

}

void GERM2Index::pointerPressEvent(MAPoint2d point){
	Point p;
	p.set(point.x, point.y);

	if (mItems->contains(p)){
		for(int i=0; i< mItems->getChildren().size(); i++){
			if (mItems->getChildren()[i]->contains(p)){
				int index = mItems->getSelectedIndex();
				if (index == i) keyPressEvent(MAK_FIRE, 0);
				else mItems->setSelectedIndex(i);
				break;
			}
		}
	}
	else if (mSoftkey->contains(p)){
		if(mSoftkey->getChildren()[0]->contains(p)) keyPressEvent(MAK_SOFTLEFT, 0);
		else if (mSoftkey->getChildren()[2]->contains(p)) keyPressEvent(MAK_SOFTRIGHT, 0);
	}
}

void GERM2Index::setInput(String& companyid, String& digits, const String& title){
	mCompanyid = companyid;
	mDigits = digits;
	mTitle->setCaption(title);
	mTitle->update();
	mProcessor->setInput(mCompanyid, mDigits);
}

void GERM2Index::setIsGetData(bool value){
	isGetData = value;
}

bool GERM2Index::getIsGetData(){
	return isGetData;
}
//http://203.162.70.245:8314/WebService.asmx?WSDL
